<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        border
        stripe
        :where="defaultWhere"
      >
        <template #toolbar>
          <el-button type="success" @click="openEdit(null)"> 新建 </el-button>
        </template>
        <template #plate_number="{ row }">
          <span class="blue pointer" @click="openDetail(row)">{{
            row.plate_number
          }}</span>
        </template>
        <template #create_mode="{ row }">
          <span class="orange" v-if="row.create_mode === '手动录入'">{{row.create_mode}}</span>
          <span class="green" v-else>{{row.create_mode}}</span>
        </template>
        <template #images="{ row }">
          <span class="pointer blue" @click="openImages(row, 'images')">
            查看
          </span>
        </template>
        <template #pay_images="{ row }">
          <span v-if="row.io_type == '支出' && row.status == 20 && row.create_mode == '手动录入'" class="pointer blue" @click="openImages(row, 'pay_images')">
            查看
          </span>
        </template>
        <template #status="{ row }">
          <el-tag :type="row.statusType">{{ row.statusText }}</el-tag>
        </template>
        <template #action="{ row }">
          <el-button-group>
            <el-button
              v-if="status == 90"
              type="warning"
              size="small"
              @click="openEdit(row)"
            >
              重新提交
            </el-button>
            <el-button
              type="info"
              size="small"
              @click="invalid(row)"
              v-if="status == 90"
            >
              作废
            </el-button>
            <el-button
              v-if="status != 90 && row.status != 20"
              type="warning"
              size="small"
              @click="openEdit(row)"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              size="small"
              @click="del(row)"
              v-if="status != 90 && row.status != 20"
            >
              删除
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>

    <!-- 车辆详情 -->
    <detail v-model="showDetail" :data="current" />
    <edit v-model="showEdit" :data="current" @done="reload" />
    <images v-model="showImages" :data="current" :img-type="imagesType" />
  </ele-page>
</template>

<script setup>
  import { ref, computed } from 'vue';
  import Search from './components/search.vue';
  import {
    delVehicleIncomeExpense,
    pageVehicleIncomeExpense,
    invalidVehicleIncomeExpense
  } from '@/api/company';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessage, ElMessageBox } from 'element-plus/es';
  import Detail from '../components/detail.vue';
  import Images from './components/images.vue';

  import Edit from './components/cost.vue';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 列表的列 */
  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];

    cols.push(
      {
        prop: 'id',
        label: 'ID',
        width: 80
      },
      {
        align: 'center',
        width: 90,
        slot: 'status',
        label: '状态'
      }
    );
    if (status.value == 90) {
      cols.push({
        prop: 'reject_reason',
        label: '不通过原因'
      });
    }
    cols.push(
      {
        width: 90,
        align: 'center',
        slot: 'create_mode',
        label: '记录类型'
      },
      {
        align: 'center',
        width: 55,
        prop: 'io_type',
        label: '收支'
      },
      {
        width: 150,
        prop: 'cate',
        label: '费用分类'
      },
      {
        width: 120,
        prop: 'amount',
        label: '金额'
      },
      {
        width: 120,
        prop: 'belongs_date',
        label: '费用日期'
      },
      {
        prop: 'remark',
        label: '费用说明'
      },
      {
        width: 90,
        align: 'center',
        slot: 'images',
        label: '相关材料'
      },
      {
        width: 90,
        align: 'center',
        slot: 'pay_images',
        label: '付款凭证'
      }
    );

    cols.push(
      {
        width: 70,
        align: 'center',
        prop: 'area',
        label: '区域'
      },
      {
        width: 120,
        slot: 'plate_number',
        label: '相关车辆'
      },
      {
        prop: 'company',
        label: '所属车队'
      }
    );
    if (status.value == 0 || status.value == 90) {
      cols.push({
        columnKey: 'action',
        label: '操作',
        slot: 'action',
        width: 150,
        align: 'center',
        fixed: 'right'
      });
    }

    return cols;
  });

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageVehicleIncomeExpense({
      ...where,
      ...orders,
      page,
      limit
    });
  };
  const defaultWhere = ref({
    cate: '不限',
    status: 99999999,
    io_type: '不限'
  });

  /** 搜索 */
  const costType = ref('支出');
  const status = ref(99999999);
  const reload = (where) => {
    costType.value = where ? where.io_type : '收入';
    status.value = where ? where.status : 99999999;
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
    console.log(status.value, 'wtfccccccccccccccc?');
  };
  /** 查看图片 */
  const showImages = ref(false);
  const imagesType = ref('');
  const openImages = (row, type) => {
    current.value = row;
    imagesType.value = type;
    showImages.value = true;
  };

  /** 车辆详情 */
  const showDetail = ref(false);
  const openDetail = (row) => {
    current.value = row ?? null;
    showDetail.value = true;
  };
  /** 删除 */
  const del = (row) => {
    ElMessageBox.confirm(
      '确定要删除' +
        row.plate_number +
        '的' +
        row.amount +
        '元的' +
        row.cate +
        row.io_type +
        '吗？',
      '系统提示',
      {
        type: 'warning',
        draggable: true
      }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        delVehicleIncomeExpense({ id: row.id })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
            reload();
          })
          .catch((e) => {
            console.log(e);
            loading.close();
          });
      })
      .catch(() => {});
  };
  /** 作废申请 */
  const invalid = (row) => {
    ElMessageBox.confirm(
      '确定要作废' +
        row.plate_number +
        '的' +
        row.amount +
        '元的' +
        row.cate +
        row.io_type +
        '吗？',
      '系统提示',
      {
        type: 'warning',
        draggable: true
      }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        invalidVehicleIncomeExpense({ id: row.id })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
            reload();
          })
          .catch((e) => {
            console.log(e);
            loading.close();
          });
      })
      .catch(() => {});
  };

  /** 新建 */
  const showEdit = ref(false);
  const openEdit = (row) => {
    current.value = row ?? null;
    if (current.value && current.value.status == 20) {
      ElMessage.warning('已完成状态的收支无法编辑');
      return;
    }
    showEdit.value = true;
  };
</script>
